﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AppFx.Tokenization;

namespace Tests.Tokenization
{
    [TestClass]
    public class TokenizerTest
    {
        TokenizedExpressionValidator validator;

        public TokenizerTest()
        {
            Tokenizer tokenizer = new Tokenizer();
            tokenizer.Add(new StringVerifier(tokenizer));
            tokenizer.Add(new WhitespaceVerifier(tokenizer));
            tokenizer.Add(new NumberVerifier(tokenizer));
            tokenizer.Add(new SymbolVerifier(tokenizer, '+', '-', '*', '/', '&', '|', '^'));
            tokenizer.Add(new BooleanVerifier(tokenizer));
            validator = new TokenizedExpressionValidator(tokenizer);
        }

        private List<TokenExpression> CreateExpressions()
        {
            //Expressions.Add("-9,587 + 8.515.515 - A75.933.221.321,58 * +2 / 6,9");
            //Expressions.Add("-9,587 + 8.515.515 - A75.933.221.32.1,58 * +2 / 6,9");
            //Expressions.Add("true false + 5 / 5,2");
            //Expressions.Add("9 * 1 + 3 / 4");
            //Expressions.Add("909 * 10 + 3 / 45");
            //Expressions.Add("90,9 * 1,0 + 3 / 4.5");
            //Expressions.Add("1 / 2");
            //Expressions.Add("1 * 2");
            //Expressions.Add("true | false + 5 / 5,2");

            List<TokenExpression> expressions = new List<TokenExpression>();

            expressions.Add(new TokenExpression("1 + 2",
                eTokenType.NumberLiteral,
                eTokenType.Whitespace,
                eTokenType.Symbol,
                eTokenType.Whitespace,
                eTokenType.NumberLiteral
            ));

            expressions.Add(new TokenExpression("'abc' 95801 'def' 56798",
                eTokenType.StringLiteral,
                eTokenType.Whitespace,
                eTokenType.NumberLiteral,
                eTokenType.Whitespace,
                eTokenType.StringLiteral,
                eTokenType.Whitespace,
                eTokenType.NumberLiteral
            ));

            expressions.Add(new TokenExpression("9587 + 851 - 759 * 2 / 6",
                eTokenType.NumberLiteral, //9587
                eTokenType.Whitespace,
                eTokenType.Symbol, //+
                eTokenType.Whitespace,
                eTokenType.NumberLiteral, //851
                eTokenType.Whitespace,
                eTokenType.Symbol, //-
                eTokenType.Whitespace,
                eTokenType.NumberLiteral, //759
                eTokenType.Whitespace,
                eTokenType.Symbol, //*
                eTokenType.Whitespace,
                eTokenType.NumberLiteral, //2
                eTokenType.Whitespace,
                eTokenType.Symbol, //
                eTokenType.Whitespace,
                eTokenType.NumberLiteral
            ));

            expressions.Add(new TokenExpression("'abc' 'def'",
                eTokenType.StringLiteral,
                eTokenType.Whitespace,
                eTokenType.StringLiteral
            ));

            return expressions;
        }

        [TestMethod]
        public void Test_Expressions()
        {
            List<TokenExpression> expressions = CreateExpressions();

            foreach (TokenExpression expression in expressions)
                Assert.IsTrue(validator.Validate(expression));
        }
    }
}
